package com.study.advice.demo;

import com.study.advice.demo.after.AfterAdvice;
import com.study.advice.demo.around.AroundAdvice;
import com.study.advice.demo.bean.ServiceDemo;
import com.study.advice.demo.bean.ServiceDemoImpl;
import com.study.advice.demo.before.BeforeAdvice;
import com.study.advice.demo.introduction.ControllablePerformanceMonitor;
import com.study.advice.demo.introduction.Monitorable;
import com.study.advice.demo.throwing.ThrowingAdvice;
import org.springframework.aop.framework.ProxyFactory;

public class TestAdvice {

    public static void main(String[] args) {
        //接口类
        ServiceDemo serviceDemo = new ServiceDemoImpl();
        //advice 类
        BeforeAdvice beforeAdvice = new BeforeAdvice();
        AfterAdvice afterAdvice = new AfterAdvice();
        AroundAdvice aroundAdvice = new AroundAdvice();
        ThrowingAdvice throwingAdvice = new ThrowingAdvice();
        ControllablePerformanceMonitor controllablePerformanceMonitor = new ControllablePerformanceMonitor();
        //创建代理工厂
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(serviceDemo);//设置代理目标

        //proxyFactory.addAdvice(beforeAdvice);//设置增强 前置

        //proxyFactory.addAdvice(afterAdvice);//设置增强 后置

        //proxyFactory.addAdvice(aroundAdvice);//设置增强 环绕

        //proxyFactory.addAdvice(throwingAdvice);//设置增强 异常

        proxyFactory.addAdvice(controllablePerformanceMonitor);//设置增强 引介
        //引介 增强必须设置
        proxyFactory.setInterfaces(Monitorable.class);
        proxyFactory.setProxyTargetClass(true);

        //生成代理类
        ServiceDemoImpl proxy = (ServiceDemoImpl) proxyFactory.getProxy();
        //引介 增强必须设置
        Monitorable monitorable = (Monitorable) proxy;
        monitorable.setActive(true);

        Long userId = 1L;
        proxy.selcet(userId);
    }
}
